<?

class Smutty_Data {

	var $data;

	/**
	 *  constructor
	 *
	 */

	function Smutty_Data( $data = false ) {

		if ( $data )
			$this->data = $data;
		else
			$this->data = ( $_SERVER['REQUEST_METHOD'] == 'POST' )
				 ? $_POST : $_GET;

	}

	/**
	 *  returns a data object for $_POST
	 *
	 */

	function getPostData() {
		return new Smutty_Data( $_POST );
	}

	/**
	 *  returns a data object for $_GET
	 *
	 */

	function getGetData() {
		return new Smutty_Data( $_GET );
	}

	/**
	 *  returns a data object for $_SERVER
	 *
	 */

	function getServerData() {
		return new Smutty_Data( $_SERVER );
	}

	/**
	 *  used to set values for this object
	 *
	 *  @param [name] the name of the param
	 *  @param [value] it's value
	 *
	 */

	function set( $name, $value ) {
		$this->data[ $name ] = $value;
	}

	/**
	 *  STATIC
	 *
	 *  static method for fetching the instance of this class (as
	 *  it's designed to be a singleton)
	 *
	 *  @return Smutty_Data the class instance
	 *
	 */

	function &getInstance() {
		static $instance;
		if ( $instance == null )
			$instance = new Smutty_Data();
		return $instance;
	}

	/**
	 *  returns the value of the specifed variable as
	 *  an integer (false if it's not valid)
	 *
	 *  @param [name] name of variable to fetch
	 *  @return int integer value
	 *
	 */

	function int( $name ) {
		return Smutty_Data::getInt( $this->data[$name] );
	}

	/**
	 *  this method can be used staticly to validate data
	 *
	 *  @param [value] the value to validate
	 *  @return int
	 *
	 */

	function getInt( $value ) {
		return (int) $value;
	}

	/**
	 *  returns an integer with a padded 0 if it
	 *  is less than 10
	 *
	 *  @param [name] the name of the variable
	 *  @return int
	 *
	 */

	function intPad( $name ) {
		$value = $this->int( $name );
		return ( $value < 10 ) ? "0$value" : $value;
	}

	/**
	 *  returns the value of the specifed variable
	 *  as a string.
	 *
	 *  @param [name] name of variable
	 *  @return String string value
	 *
	 */

	function string( $name ) {
		return $this->data[ $name ];
	}

	/**
	 *  returns a data object, which is a new instance of
	 *  this object.  this will be something like args passed
	 *  in from a form using name[value] type args
	 *
	 *  @param [name] the name of the data array
	 *  @return Smutty_Data
	 *
	 */

	function data( $name ) {
		return new Smutty_Data( $this->data[$name] );
	}

	/**
	 *  returns the fields that exist in this data model
	 *
	 *  @return array
	 *
	 */

	function getFields() {
		$fields = array();
		foreach ( $this->data as $name => $value )
			array_push( $fields, $name );
		return $fields;
	}

	/**
	 *  returns the current date and time formatted as a string
	 *  for use with the database
	 *
	 *  @return String
	 *
	 */

	function getDate() {
		$db = Smutty_Database::getInstance();
		return $db->getCurrentDate();
	}

}

?>